<?php 
Class adminController extends appController
{
	/**
	 * 管理员列表
	 */
	public function index()
	{		
	   	$page = yf_empty($_REQUEST['page'], 1, true);
		$user_list = Model('admin')->adminList($page);
		$this->assign('user_list',$user_list['data']);
		$this->assign('user_list_page',$user_list['pagelink']);
		$this->display('index.html');
	}
	
	/**
	 * 新建管理员页面
	 */
	public function add_admin()
	{
		$this->admin_priv('addmem');
		$user_group=Model('admin')->get_role_list();
		$this->assign('action','insert_user');
		$this->assign('user_group',$user_group);
		$this->display('add_user.html');
	}
	
	/**
	 * 插入管理员信息
	 */
	public function insert_user()
	{
		$this->admin_priv('addmem', 2);
		
		$res = array('error' => 1, 'content' => '');
		
		$info['username'] = yf_empty(trim($_POST['username']), "用户名不能为空");
		$info['qq'] = yf_empty(intval($_POST['qq']), "QQ不能为空");
		$info['email'] = yf_empty($_POST['email'], "邮箱不能为空");
		$info['group_id'] = yf_empty($_POST['group_id'], "请选择管理身份");
		$info['password'] = yf_empty($_POST['new_password'], "密码不能为空");
		$confirm_password = yf_empty($_POST['confirm_password'], "确认密码不能为空");
	
		if($info['password'] != $confirm_password)
		{
			$res['content'] = Lang('twice_pwd_not_match');
			$this->showMsg($res);
		}
		
		if(strlen($info['password']) < 6)
		{
			$res['content'] = Lang('pwd_too_short');
			$this->showMsg($res);
		}
		
		if(hy_check($info['email'], 'e') == '#@_error')
		{
			$res['content'] = Lang('email_format_error');
			$this->showMsg($res);
		}
		 
		$check = Model('admin')->isExist($info);
		if($check)
		{
			$res['content'] = "该用户名或者邮箱已经存在";
			$this->showMsg($res);
		}
		else
		{
			$info['password'] = md5($info['password']);
			$info['status'] = 0;
			$info['created'] = yf_time();
			if(Model('admin')->insertInfo($info))
			{
				$res = array('error' => 0, 'content' => Lang('SUCCESS_ADD'));
			}
			else
			{
				$res['content'] = Lang('INVALID_OPERATION');
			}
		}
		$this->showMsg($res);
	}
	
	/**
	 * 显示管理员编辑界面
	 */
	public function edit_admin()
	{
		$this->admin_priv('editmem');
		$id = yf_empty($_GET['id'], 0, true);
		$user_info = Model('admin')->getAdminInfo($id);
		$this->assign('user_info', $user_info);
		$user_group=Model('admin')->get_role_list();
		$this->assign('id',$id);
		$this->assign('action', 'update_admin');
		$this->assign('user_group', $user_group);
		$this->display('add_user.html');
	}
	
	/**
	 * 更新管理员编辑信息
	 */
	public function update_admin()
	{
		$res = array('error' => 1, 'content' => '');
		$id = yf_empty($_POST['id'], 0, true);
		$info['username'] = yf_empty(trim($_POST['username']), "用户名不能为空");
		$info['qq'] = yf_empty(intval($_POST['qq']), "QQ不能为空");
		$info['email'] = yf_empty($_POST['email'], "邮箱不能为空");
		$info['group_id'] = yf_empty(intval($_POST['group_id']), "请选择管理组");
		$info['password'] = yf_empty(trim($_POST['new_password']), "", true);
		$confirm_password = yf_empty(trim($_POST['confirm_password']), "", true);
	
		if(!empty($info['password']) && !empty($confirm_password))
		{
			if($confirm_password != $info['password'])
			{
				$res['content'] = Lang('twice_pwd_not_match');
				$this->showMsg($res);
			}
			
			if(strlen($info['password']) < 6)
			{
				$res['content'] = Lang('PWD_TOO_SHORT');
				$this->showMsg($res);
			}
			$info['password'] = md5($info['password']);
		}
		else
		{
			unset($info['password']);
		}
	
		if(hy_check($info['email'], "e") == '#@_error')
		{
			$res['content'] = Lang('EMAIL_FORMAT_ERROR');
			$this->showMsg($res);
		}
		
		if(Model('admin')->checkInfo($info['username'], $info['email'], $id))
		{
			$info['modified'] = yf_time();
			if(Model('admin')->updateInfo($info,$id))
			{
				$res = array('error' => 0, 'content' => Lang('SUCCESS_EDIT'));
			}
			else
			{
				$res['content'] = Lang('INVALID_OPERATION');
			}
		}
		else
		{
			$res['content'] = "用户名或者邮箱已被使用";
		}
		$this->showMsg($res);
	}
	
	/**
	 * 删除管理员
	 */
	public function del_admin()
	{
		$res = array('error' => 1, 'content' => '删除失败');
		$this->admin_priv('delmem', 2);
		$id = yf_empty($_POST['id'], 0, true);
	
		$result=Model('admin')->del_user($id);
		if($result)
		{
			if($_SESSION['admin_id'] == $id)
			{
				$this->logout(1);
			}
			$res = array('error' => 0, 'content' => '删除成功');
		}
		$this->showMsg($res);
	}
	
	/**
	 * 角色管理列表
	 */
	public function role()
	{
		$page = yf_empty($_REQUEST['page'], 1, true);
		$user_list=Model('admin')->roleList($page);
		$this->assign('user_list',$user_list['result']);
		$this->assign('user_list_page',$user_list['page']);
		$this->display('role_list.html');
	
	}
	
	/**
	 * 添加角色页面
	 */
	public function add_role()
	{
		$this->admin_priv('adduser');
		$action_list=Model('admin')->get_admin_action();
		$this->assign('action','new_role');
		$this->assign('action_list',$action_list);
		$this->display('role.html');
	}
	
	/**
	 * 添加角色数据
	 */
	public function new_role()
	{
		$this->admin_priv('adduser', 2);
		$res = array('error' => 1, 'content' => '');
		$info['role_name'] = yf_empty($_POST['role_name'], "角色名不能为空");
		$info['role_des'] = yf_empty($_POST['role_des'], "", true);
		$mod = yf_empty($_POST['action_mod'], "", true);
		$action = yf_empty($_POST['action_list'], "", true);
	
		$info['role_mod'] = is_array($mod) ? implode(",", $mod) : $mod;
		$info['role_action'] = is_array($action) ? implode(",", $action) : $action;
	
		$info['created'] = yf_time();
		$info['status'] = 0;
	
		if(Model('admin')->roleEisted($info['role_name']))
		{
			if(Model('admin')->insertRole($info))
			{
				$res = array('error' => 0, 'content' => Lang('SUCCESS_ADD'));
			}
			else
			{
				$res['content'] = Lang('INVALID_OPERATION');
			}
		}
		else
		{
			$res['content'] = Lang('ROLE_EXISTED');
		}
		$this->showMsg($res);
	}
	
	/**
	 * 显示编辑角色的页面
	 */
	public function edit_role()
	{
		$this->admin_priv('edituser');
		$id = yf_empty($_GET['id'], 0, true);
		
		$user_info = Model('admin')->getRoleInfo($id);
		$action_list=Model('admin')->get_admin_action();
		$mod = explode(",", $user_info['role_mod']);
		$action = explode(",", $user_info['role_action']);
		foreach($action_list as $k => $v)
		{
			if(in_array($v['action_id'], $action))
			{
				$action_list[$k]['check'] = 1;
			}
			else 
			{
				$action_list[$k]['check'] = 0;
			}
			
			foreach($v['child'] as $kk => $vv)
			{
				if(in_array($vv['mod_id'], $mod))
				{
					$action_list[$k]['child'][$kk]['check'] = 1;
				}
				else 
				{
					$action_list[$k]['child'][$kk]['check'] = 0;
				}
			}
		}
		
		$this->assign('action_list',$action_list);
		$this->assign('user_info',$user_info);
		$this->assign('id',$id);
		$this->assign('action','update_role');
		$this->display('role.html');
	}
	
	/**
	 * 编辑角色信息
	 */
	public function update_role()
	{
		$this->admin_priv('edituser', 2);
		$res = array('error' => 1, 'content' => '');
		
		$id = yf_empty($_POST['id'], "系统错误");
		
		$info['role_name'] = yf_empty($_POST['role_name'], "角色名不能为空");
		$info['role_des'] = yf_empty($_POST['role_des'], "", true);
		$mod = yf_empty($_POST['action_mod'], "", true);
		$action = yf_empty($_POST['action_list'], "", true);
	
		$info['role_mod'] = is_array($mod) ? implode(",", $mod) : $mod;
		$info['role_action'] = is_array($action) ? implode(",", $action) : $action;
		$info['modified'] = yf_time();
	
		if(!Model('admin')->roleEisted($info['role_name'], $id))
		{
			$res['content'] = Lang('ROLE_EXISTED');	
		}
		else
		{
			if(Model('admin')->updateRole($info,$id))
			{
				$res = array('error' => 0, 'content' => Lang('SUCCESS_EDIT'));
			}
			else
			{
				$res['content'] = Lang('INVALID_OPERATION');
			}
		}
		$this->showMsg($res);
	}
	
	/**
	 * 删除角色
	 */
	public function del_role()
	{
		$res = array('error' => 1, 'content' => '');
		$this->admin_priv('deluser', 2);
		$id = yf_empty($_GET['id'], "系统错误");
		$check = Model('admin')->del_role($id);
		if($check)
		{
			$res = array('error' => 0, 'content' => '成功删除角色');
		}
		$this->showMsg($res);
	}
	
	/**
	 * 退出登录
	 * @param number $ajax
	 */
	public function logout($ajax=0)
	{
		cookie('QIAQIA[admin_id]',   '');
		cookie('QIAQIA[admin_pass]', '');

		session('[destroy]');
		$result=array('error' => '-1', 'content' => '成功退出','url'=>'/xy/qq_admin.php');
		$this->showMsg($result);
	}
	
	/**
	 * 用户信息设置页面
	 */
	public function userinfo()
	{
		$userInfo=Model('admin')->getAdminInfo();
		
		$this->assign('user_info',$userInfo);
		$this->display('userinfo.html');
	}
	
	public function update_info()
	{
		$info['username']=isset($_POST['username'])?trim($_POST['username']):'';
	    $info['qq']=isset($_POST['qq'])?intval(($_POST['qq'])):'';
	    $info['email']=isset($_POST['email'])?$_POST['email']:'';
	    $info['password']=isset($_POST['new_password'])?trim($_POST['new_password']):'';
	    $confirm_password=isset($_POST['confirm_password'])?trim($_POST['confirm_password']):'';
	    $result=array('error'=>1,'content'=>'');
	    if( $info['password']!=''&&$confirm_password!='')
	    {
	       if($confirm_password!=$info['password'])
	      {
	    	$result['content']=Lang('TWICE_PWD_NOT_MATCH');
	    	$this->showMsg($result);
	      }
	    }
	    
	   if( Model('admin')->checkInfo($info['username'],$info['email']))
	   {
	   	$res=Model('admin')->saveInfo($info);
	   	   
	   	    if($res['error']<0)
	   	    {
	   	    	$result['error']=0;
	   	     	$result['content']=Lang('SUCCESS_EDIT');
	   	    }
	   	    elseif($res['error']==0)
	   	   {
	   	      	$result['error']=0;
	   	    	 $result['content']=Lang('SUCCESS_EDIT');
	   	   }
	   	   else 
	   	   {
	   	   	$result['content']=Lang('INVALID_OPERATION');
	   	   }
	   	   $this->showMsg($result);
	   }
	}
	
	/**
	 * 显示权限栏目列表
	 */
	public function act_list()
	{
		$page = yf_empty($_REQUEST['page'], 1, true);
		$list = Model('admin')->get_action_list($page);
		$this->assign("list", $list['result']);
		$this->assign("page", $list['page']);
		$this->display("act_list.html");
	}
	
	/**
	 * 显示权限栏添加页面
	 */
	public function add_action()
	{
		$this->assign("action", "insert_action");
		$this->display("add_action.html");
	}
	
	/**
	 * 新增权限栏目处理方法
	 */
	public function insert_action()
	{
		$res = array('error' => 1, 'content' => '');
		$info['action_list'] = yf_empty($_POST['action_list'], "权限栏目不能为空");
		$info['action_child'] = 0;
		if($id = Model('admin')->insert_action($info))
		{
			$res = array('error' => 0, 'content' => '添加成功');
		}
		else
		{
			$res['content'] = "添加失败";
		}
		$this->showMsg($res);
	}
	
	/**
	 * 显示权限栏目编辑页面
	 */
	public function edit_action()
	{
		$action_id = $_REQUEST['id'];
		$action_list = Model('admin')->get_action_info($action_id);
		$this->assign("action_id", $action_id);
		$this->assign("action_list", $action_list);
		$this->assign("action", "update_action");
		$this->display("add_action.html");
	}
	
	/**
	 * 编辑权限栏目数据处理
	 */
	public function update_action()
	{
		$res = array('error' => 0, 'content' => '编辑成功');
		$action_id = yf_empty($_POST['action_id'], "系统错误");
		$action_list = yf_empty($_POST['action_list'], "权限栏目不能为空");
		Model('admin')->update_action($action_list, $action_id);
		$this->showMsg($res);
	}
	
	/**
	 * 权限栏目的权限详情列表
	 */
	public function view_action()
	{
		$id = $_REQUEST['id'];
		$page = yf_empty($_REQUEST['page'], 1, true);
		$list = Model('admin')->get_mod_list($id, $page);
		$this->assign("id", $id);
		$this->assign("list", $list['result']);
		$this->assign("page", $list['page']);
		$this->display("view_action.html");
	}
	
	/**
	 * 显示添加详细的权限功能页面
	 */
	public function add_act()
	{
		$id = $_REQUEST['id'];
		$this->assign("id", $id);
		$this->assign("action", "ins_act");
		$this->display("add_act.html");
	}
	
	/**
	 * 添加详细的权限功能
	 */
	public function ins_act()
	{
		$res = array('error' => 1, 'content' => '');
		$info['action_id'] = yf_empty($_POST['action_id'], "系统错误");
		$info['mod_name'] = yf_empty($_POST['mod_name'], "权限功能不能为空");
		$info['mod_code'] = yf_empty($_POST['mod_code'], "权限标识不能为空");
		$info['status'] = yf_empty($_POST['status'], 1, true);
		if($id = Model('admin')->ins_act($info))
		{
			$res = array('error' => 0, 'content' => '添加成功');
		}
		else
		{
			$res['content'] = "添加失败";
		}
		$this->showMsg($res);
	}
	
	/**
	 * 显示编辑详细的权限功能页面
	 */
	public function edit_mod()
	{
		$id = $_REQUEST['id'];
		$info = Model('admin')->get_one_mod($id);
		$this->assign("info", $info);
		$this->assign("action", "ud_act");
		$this->display("add_act.html");
	}
	
	/**
	 * 修改详细的权限功能
	 */
	public function ud_act()
	{
		$res = array('error' => 0, 'content' => '编辑成功');
		$id = yf_empty($_POST['mod_id'], "系统错误");
		$info['mod_name'] = yf_empty($_POST['mod_name'], "权限功能不能为空");
		$info['mod_code'] = yf_empty($_POST['mod_code'], "权限标识不能为空");
		Model('admin')->ud_act($info, $id);
		$this->showMsg($res);
	}
	
}